#include <stats.h>

#include <sstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <string>
#include <stdlib.h>

stats::stats() {
	// nullop
}

void stats::setName( std::string name ) {
	this->stat_name = this->toLower(name);
}

void stats::setValue( std::string value ) {
	this->stat_value = value;
}

void stats::setType( std::string type ) {
	this->stat_type = type;
}

std::string stats::getName() {
	return this->toLower(this->stat_name);
}

std::string stats::getValue() {
	return this->stat_value;
}

std::string stats::getType() {
	return this->stat_type;
}

bool stats::processValue() {
	int chancer = 1000;


	// OK. We're going to hardcode some goodies in.
	// This will override "smart" behavior
	//

	std::string type = this->getType();

	// this should be overridden as needed
	// we're going to process in reverse order.
	// there are a few "classes" of data we have

	// class "one"     data: ".nnn".         This is counted as a chance statistic
	// class "two"     data: "nnn".          This is counted as a chance statstic ( if between 0 -- 100 )

	int class_two_max = 100;
	int class_two_min = 0;

	// for class 1 & 2, do a "roll".

	// class "three"   data: "nnnn".         This is counted as an "if exists" 
	// class "four"    data: "true / false"  This is counted as a boolean
	// class "five"    data: "alpha-data".   This is counted as an "if exists"

	// for class 3, 4, 5, do a raw process.

	double class_one = atof( this->getValue().c_str() ); // doubles are fricken huge.

	if ( class_one != 0 && class_one < 1 ) // atof returns 0 on error. WTF, seriously?
		return ( ( ( rand() % chancer ) < ( class_one * chancer ) ));

	if ( class_one > class_two_min && class_one < class_two_max ) // play cleanup
		return ( ( ( rand() % chancer ) < ( ( class_one / class_two_max ) * chancer ) ));

	// OK. End the "easy" stuff.

	if ( class_one > class_two_max )
		return true; // we've got data above chance max.

	std::string data = this->toLower(this->getValue());

	if ( data == "true" )
		return true; // true things tend to be true
	if ( data == "false" )
		return false; // as do false things, false to be are.

	if ( data != "" )
		return true; // if we have *any* saving grace

	return false;
}


std::string stats::toLower(std::string str) {
	int len = str.length();
	for ( int i = 0; i < len; ++i)
		if ( 65 <= str[i] && str[i] <= 90 ) // A-Z
			str[i]+= 32; // a-z
	return str;
}

std::string stats::toString() {
	std::string ret;

	ret = "";
	ret.append( "(" + this->getType() + ") " );
	ret.append(this->getName() );
	ret.append(" = ");
	ret.append(this->getValue());
	ret.append("\n\r"); // NOTE: ENDL WILL REALLY
	                    // SCREW YOU OVER HERE.

	return ret;
}

